import Constants from '@/components/hhd/design/FormEngine/Constants';

export default class FormEngineContext {
  vue;
  formDefinition;
  viewId;
  actionId;
  formModel;
  emit;
  setProperty;

  //下面是运行时产生的数据
  view;
  action;
  templateConfigs;
  h;

  constructor(vue) {
    this.vue = vue;
    this.formDefinition = vue.$props.formDefinition;
    this.viewId = vue.$props.view;
    this.actionId = vue.$props.action;
    this.formModel = vue.$props.formModel;
    this.emit = vue.$emit;
    this.setProperty = vue.$set;
    this.h = vue.$createElement;

    this.initIfNecessary();
  }

  initIfNecessary() {
    this.view = this.formDefinition.views.find(v => v.id === this.viewId);
    this.action = this.formDefinition.actions.find(a => a.id === this.actionId);
    this.templateConfigs = this.view && this.view.templateConfigs || undefined;
    this.buildModelIfNecessary();
  }

  buildModelIfNecessary() {
    if (!this.formModel) {
      this.vue.$emit('change', {});
    } else {
      this.buildMainFields();
      this.buildSubFormFields();
      this.buildMainAuthFields();
      this.buildSubAuthFields();
    }
  }

  buildMainAuthFields() {
    this.doBuildAuthFields(this.formModel, this.formDefinition);
  }

  buildSubAuthFields() {
    let subForms = this.formDefinition.subForms || [];
    subForms.forEach(subForm => {
      let subFormModels = this.formModel[subForm.id] || [];
      subFormModels.forEach(subFormModel => {
        this.doBuildAuthFields(subFormModel, subForm);
      });
    });
  }

  doBuildAuthFields(model, formDefinition) {
    let auth = model[Constants.authFieldName] || {};
    let fields = formDefinition && formDefinition.fields || [];
    fields.forEach(f => {
      let fieldActions = this.action && this.action.fieldActions || [];
      let fieldAuth = 'edit';
      fieldActions.forEach(fa => {
        if (fa.fieldId === f.id) {
          fieldAuth = fa.auth;
        }
      });
      this.setProperty(auth, f.fieldName, auth[f.fieldName] || fieldAuth || 'edit');
    });
    this.setProperty(model, Constants.authFieldName, auth);
  }

  buildMainFields() {
    this.buildFields(this.formDefinition, this.formModel);
  }

  buildSubFormFields() {
    let subForms = this.formDefinition.subForms || [], model = this.formModel;
    subForms.forEach(subForm => {
      let subFormDataSource = model[subForm.id] || [];
      this.setProperty(model, subForm.id, subFormDataSource);
      subFormDataSource.forEach(item => {
        this.buildFields(subForm, item);
      });
    });
  }

  buildFields({fields}, model) {
    fields = fields || [];
    fields.forEach(f => {
      this.setProperty(model, f.fieldName, model[f.fieldName] || undefined);
      f.fieldTitleName && this.setProperty(model, f.fieldTitleName, model[f.fieldTitleName]);
    });
  }

  check() {
    return !!this.view && !!this.view && !!this.view.templateConfigs;
  }

  resetAction() {
    let temp = {...this.formModel};
    temp._auth = undefined;

    let subForms = this.formDefinition.subForms || [], model = temp;
    subForms.forEach(subForm => {
      let subFormDataSource = model[subForm.id] || [];
      this.setProperty(model, subForm.id, subFormDataSource);
      subFormDataSource.forEach(item => {
        item._auth = undefined;
        this.buildFields(subForm, item);
      });
    });
    this.vue.$emit('change', temp);
  }
}